//Mark O Meara
//08332240
//CS2041 - Assignment
//Due: 18/02/10

import javax.swing.JOptionPane;

public class App {
	public static void main(String[] args){
		
		String filename = JOptionPane.showInputDialog("Enter filename (usually A01.txt):\n");
		//ID Sort may not be working
		SerialFile sfile = new SerialFile(filename);
		//Get instruction from user
		String instruction = "z"; //default
		do{
			instruction = JOptionPane.showInputDialog("Enter 'a' to add a record,\nEnter 'b' to delete record by ID,\n" +
					"Enter 'c' to delete all records with a value,\nEnter 'd' to update one field,\nEnter 'e' to search,\nEnter 'f' to print a record,\n"+
					"Enter 'g' to compress,\nEnter 'h' to sort,\nEnter 'i' to change delimeters\nEnter 'z' to exit!");
	
			/*Adding new record*/
			if(instruction.equalsIgnoreCase("a")){
				int recID = Integer.parseInt(JOptionPane.showInputDialog("Enter ID for new record:\n"));
				String lastname = JOptionPane.showInputDialog("Enter last name of new record:\n");
				String firstname = JOptionPane.showInputDialog("Enter first name of new record:\n");
				String position = JOptionPane.showInputDialog("Enter position of new record:\n");
				int del = Integer.parseInt(JOptionPane.showInputDialog("Enter 1 to mark for deletion, 0 otherwise\n"));
				boolean X = false;
				if(del == 1){X = true;}
				boolean success = sfile.addRecord(recID, lastname, firstname, position, X);
				if(success == true){
					JOptionPane.showMessageDialog(null,"Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(null,"Not Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
			}
			/*Delete record*/
			else if(instruction.equalsIgnoreCase("b")){
				int ID = Integer.parseInt(JOptionPane.showInputDialog("Enter ID to delete:\n"));
				boolean success = sfile.deleteRecord(ID);
				if(success == true){
					JOptionPane.showMessageDialog(null,"Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(null,"Not Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
			}
			
			/*Delete Records*/
			else if(instruction.equalsIgnoreCase("c")){
				String fieldname = JOptionPane.showInputDialog("Enter fieldname:\n");
				String value = JOptionPane.showInputDialog("Enter Value\n");
				boolean success = sfile.deleteRecords(fieldname, value);
				if(success == true){
					JOptionPane.showMessageDialog(null,"Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(null,"Not Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
			}
			/*Update Field*/
			else if(instruction.equalsIgnoreCase("d")){
				int ID = Integer.parseInt(JOptionPane.showInputDialog("Enter ID: "));
				String fieldname = JOptionPane.showInputDialog("Enter fieldname: ");
				String value = JOptionPane.showInputDialog("Enter value: ");
				boolean success = sfile.updateField(ID, fieldname, value);
				if(success == true){
					JOptionPane.showMessageDialog(null,"Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
				else{
					JOptionPane.showMessageDialog(null,"Not Completed Successfully","Result",JOptionPane.INFORMATION_MESSAGE);
				}
			}
			/*Search Record*/
			else if(instruction.equalsIgnoreCase("e")){
				String fieldname = JOptionPane.showInputDialog("Enter fieldname: \n");
				String value = JOptionPane.showInputDialog("Enter Value: \n");
				int result = sfile.searchRecord(fieldname, value);
				JOptionPane.showMessageDialog(null,"Found: " + result,"Result",JOptionPane.INFORMATION_MESSAGE);
			}
			/*Print Record*/
			else if(instruction.equalsIgnoreCase("f")){
				int ID = Integer.parseInt(JOptionPane.showInputDialog("Enter ID:\n"));
				sfile.printRecord(ID);
			}
			/*Compress*/
			else if(instruction.equalsIgnoreCase("g")){
				sfile.compress();
			}
			/*Sort*/
			else if(instruction.equalsIgnoreCase("h")){
				String fieldname = JOptionPane.showInputDialog("Enter field:\n");
				sfile.sort(fieldname);
			}
			/*Change Delimiters*/
			else if(instruction.equalsIgnoreCase("i")){
				String valdelim = JOptionPane.showInputDialog("Enter value delimiter:\n");
				String fielddelim = JOptionPane.showInputDialog("Enter field delimiter:\n");
				String recdelim = JOptionPane.showInputDialog("Enter record delimiter:\n");
				sfile.changeDelim(valdelim,fielddelim,recdelim);
			}
		}while(!instruction.equalsIgnoreCase("z"));
	}
}
